package com.taptech.doufu.ui.adapter;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.model.novel.bean.NovelCategoryGroupBean;

import java.util.List;

/**
 * author zhangyongcun
 * 16/9/29
 * novel rank left item 适配器
 */
public class NovelCategoryGroupAdapter extends RecyclerView.Adapter<NovelCategoryGroupAdapter.ViewHolder> {

    List<NovelCategoryGroupBean> itemBeanList;
    private int selTextColor;
    private int normalColor;
    private int tvSelectBg;
    private int tvNormalBg;
    private int selPosition = 0;
    private OnItemClick onItemClick;


    public void setOnItemClickListen(OnItemClick onItemClick){
        this.onItemClick = onItemClick;
    }

    public NovelCategoryGroupAdapter(List<NovelCategoryGroupBean> itemBeanList) {
        this.itemBeanList = itemBeanList;
        initTextColor();
    }

    @Override
    public NovelCategoryGroupAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.novle_category_left_item_layout, parent, false);
        return new NovelCategoryGroupAdapter.ViewHolder(view);
    }

    private static final String TAG = NovelCategoryGroupAdapter.class.getSimpleName();

    @Override
    public void onBindViewHolder(NovelCategoryGroupAdapter.ViewHolder holder, int position) {
        NovelCategoryGroupBean itemBean = itemBeanList.get(position);
        holder.leftItemName.setText(itemBean.getTitle());
        changeTextColor(position, holder.leftItemName);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelPosition(position);
                notifyDataSetChanged();
                if (onItemClick != null) {
                    onItemClick.onClick(position,itemBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return itemBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView leftItemName;

        public ViewHolder(View itemView) {
            super(itemView);
            leftItemName = (TextView) itemView.findViewById(R.id.item_novel_rank_left);
        }
    }

    public void setSelPosition(int selPosition) {
        this.selPosition = selPosition;
    }

    private void initTextColor() {
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            selTextColor = WeMediaApplication.getInstance().getResources().getColor(R.color.theme_main_dark);
            normalColor = WeMediaApplication.getInstance().getResources().getColor(R.color.text_dark);
            tvSelectBg = R.drawable.bg_rank_select_dark;
        } else {
            selTextColor = WeMediaApplication.getInstance().getResources().getColor(R.color.text_color_17);
            normalColor = WeMediaApplication.getInstance().getResources().getColor(R.color.text_color_33);
            tvSelectBg = R.drawable.bg_rank_select;
        }
        tvNormalBg = WeMediaApplication.getInstance().getResources().getColor(R.color.transparent_background);
    }

    private void changeTextColor(int position, TextView textView) {
        if (position == selPosition) {
            textView.setTextColor(selTextColor);
            textView.setBackgroundResource(tvSelectBg);
        } else {
            textView.setTextColor(normalColor);
            textView.setBackgroundColor(tvNormalBg);
        }
    }

    public interface OnItemClick{
        void onClick(int position, NovelCategoryGroupBean bean);
    }


}
